home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / p_misc / 7p203upp.exe / V203SRC.LZH / CORRECT.C < prev    next >
C/C++ Source or Header  |  1992-07-12  |  9KB  |  360 lines

  1. #include "7plus.h"
  2. #include "globals.h"
  3.  
  4. /*
  5. ***
  6. ***
  7. ***
  8.  */
  9. int correct_meta (char *name, int itsacor)
  10. {
  11.   FILE     *rfile, *meta, *ifile;
  12.   char     inpath[MAXFPATH], indexfile[MAXPATH], metafile[MAXPATH];
  13.   char     newname[MAXPATH], orgname[13], filename[13];
  14.   char     *p, line[81];
  15.   int      i, j, corrpart, corrline, corrline2, splitsize, length;
  16.   int      num;
  17.   uint     crc, csequence;
  18.   long     binbytes, c_line, offset, oldoffset;
  19.   ulong    ftimestamp;
  20.  
  21.   *indexfile = EOS;
  22.   ftimestamp = 0UL;
  23.   splitsize = corrpart = corrline = corrline2 = num = j = 0;
  24.   crc = csequence = 0U;
  25.   binbytes = offset = 0L;
  26.   oldoffset = -62L;
  27.   rfile = ifile = meta = NULLFP;
  28.  
  29.   /* Isolate input-path and filename */
  30.   fnsplit (name, _drive, _dir, _file, _ext);
  31.   sprintf (inpath, "%s%s", _drive, _dir);
  32.   build_DOS_name (_file);
  33.   build_DOS_name (_ext);
  34.   _file[8] = _ext[3] = EOS;
  35.   strcpy (newname, name);
  36.  
  37.   printf ("\n-------------\nCorrecting...\n-------------\n\n");
  38.  
  39.   index->lines_left = 1;
  40.  
  41.   while (index->lines_left)
  42.   {
  43.     if (itsacor)
  44.     {
  45.       if (num)
  46.       {
  47.         if (stricmp( _ext, "cor") && num == 1)
  48.           break;
  49.  
  50.         if (num == 256 || (j > 4))
  51.           break;
  52.  
  53.         sprintf (newname, "%s%s%s.c%02x", _drive, _dir, _file, num);
  54.         if (test_exist (newname))
  55.         {
  56.           j++;
  57.           continue;
  58.         }
  59.         j = 0;
  60.       }
  61.       num++;
  62.  
  63.       if ((i = crc_file (newname, "7PLUS corr", " P00:\n", 1)) != 0)
  64.       {
  65.         if (i != 17)
  66.           return (i);
  67.  
  68.         if (!force)
  69.         {
  70.           printf ("\n\007If you want to use this cor-file anyway, run 7PLUS again\n");
  71.           printf ("with the '-F' option (force usage). Bear in mind, that this can\n");
  72.           printf ("cause irreparable damage to the metafile! Use at own risk.\n");
  73.  
  74.           return(7);
  75.         }
  76.       }
  77.     }
  78.     else
  79.      if (num)
  80.        break;
  81.  
  82.     /* Open COR-file */
  83.     if (!(rfile = fopen (newname, OPEN_READ_BINARY)))
  84.     {
  85.       printf (cant, newname);
  86.       return (2);
  87.     }
  88.  
  89.     while ((p = my_fgets (line, 80, rfile)) != NULL)
  90.     {
  91.       if (!strncmp (line, "7PLUS correction:", 17) && itsacor)
  92.         break;
  93.       if (!strncmp (line, " go_7+.", 7) && !itsacor)
  94.         break;
  95.     }
  96.     if (!p)
  97.     {
  98.       printf ("\007\n'%s': invalid correction file. Break.\n", newname);
  99.       fclose (rfile);
  100.       continue;
  101.     }
  102.  
  103.     if (itsacor)
  104.     {
  105.       /* Get info from COR-file */
  106.       sscanf (line, "7PLUS correction: %12s %ld %s [%lX]",
  107.                                     orgname, &binbytes, filename, &ftimestamp);
  108.       splitsize = get_hex (filename);
  109.     }
  110.     else
  111.     {
  112.       /* Get info from 7PLUS header */
  113.       if(sscanf (line+8, "%d %s %d %s %ld %s %s %s %s %s",
  114.                   &corrpart, indexfile, indexfile, orgname, &binbytes,
  115.                   indexfile, metafile, indexfile, indexfile, indexfile) == 10)
  116.       {
  117.  
  118.         if (strlen (indexfile) == 5)
  119.           if (!mcrc(line, 2))
  120.             *orgname = EOS;
  121.  
  122.         splitsize = get_hex (metafile);
  123.         strlwr (orgname);
  124.       }
  125.       else
  126.        *orgname = EOS;
  127.  
  128.       if (!*orgname)
  129.       {
  130.         printf ("\007\n'%s': Header is corrupted. Break.\n", newname);
  131.         fclose (rfile);
  132.         break;
  133.       }
  134.       offset = ftell (rfile);
  135.       fseek (rfile, -72L, SEEK_END);
  136.  
  137.       while ((p = my_fgets (line, 80, rfile)) != NULL)
  138.         if (!strncmp (line, " stop_7+.", 9))
  139.           break;
  140.  
  141.       ftimestamp = 0UL;
  142.  
  143.       if (p)
  144.       {
  145.         /* Get timespamp */
  146.         if (strchr (line, '['))
  147.         {
  148.           if (!mcrc (line, 0))
  149.             rebuild (line, 2);
  150.  
  151.           if (mcrc (line, 0))
  152.             if (sscanf (line, " stop_7+. %s [%lX]",
  153.                 indexfile, &ftimestamp) != 2)
  154.               ftimestamp = 0UL;
  155.         }
  156.       }
  157.  
  158.       fseek (rfile, offset, SEEK_SET);
  159.       offset = 0L;
  160.  
  161.       num = 1;
  162.     }
  163.  
  164.     if (num == 1)
  165.     {
  166.       /* Strip ext from filename */
  167.       fnsplit (orgname, NULL, NULL, filename, NULL);
  168.       strlwr (filename);
  169.  
  170.       sprintf (metafile , "%s.7mf", filename);
  171.  
  172.       #ifndef _CHSIZE_OK
  173.        sprintf (indexfile, "%s.7ix", filename);
  174.  
  175.        /* Open index file */
  176.        if (!(ifile = fopen (indexfile, OPEN_READ_BINARY)))
  177.        {
  178.          printf (cant, indexfile);
  179.          fclose (rfile);
  180.          return (2);
  181.        }
  182.  
  183.        /* read index info into struct index */
  184.        if (read_index (ifile, index))
  185.        {
  186.          printf ("\007Invalid index info.\n");
  187.          fclose (rfile);
  188.          return (7);
  189.        }
  190.  
  191.        fclose (ifile);
  192.       #endif
  193.  
  194.       /* Open meta file */
  195.       if (!(meta = fopen (metafile, OPEN_RANDOM_BINARY)))
  196.       {
  197.         printf (cant, metafile);
  198.         fclose (rfile);
  199.         return (2);
  200.       }
  201.  
  202.       #ifdef _CHSIZE_OK
  203.        strcpy (indexfile, metafile);
  204.        ifile = meta;
  205.  
  206.        /* read index info into struct index */
  207.        if (read_index (ifile, index))
  208.        {
  209.          printf ("\007Invalid index info.\n");
  210.          fclose (rfile);
  211.          return (7);
  212.        }
  213.       #endif
  214.     }
  215.     if (stricmp (orgname, index->filename) ||
  216.         binbytes && binbytes != index->length)
  217.     {
  218.       printf ("\007\nCorrection file '%s.%s' and metafile '%s' do not relate\n",
  219.                                                        _file, _ext, metafile);
  220.       printf (" to the same original file!\n");
  221.       fclose (rfile);
  222.       continue;
  223.     }
  224.     if ((ftimestamp && index->timestamp && (ftimestamp != index->timestamp)) &&
  225.          !force)
  226.     {
  227.       printf ("\007WARNING! The timestamps in the metafile and the correction file\n");
  228.       printf ("'%s.%s' differ!\n", _file, _ext);
  229.       printf ("If you still want to go ahead with the correction, call 7PLUS again\n");
  230.       printf ("with the addition of the '-f' option (force usage).\n");
  231.       printf ("Bear in mind, that this can cause irreparable damage to the metafile!\n");
  232.       printf ("Use at own risk.\n");
  233.       return (18);
  234.     }
  235.  
  236.     printf ("Processing '%s'. Missing lines left: %ld      \r",
  237.                                                    newname, index->lines_left);
  238.     fflush (stdout);
  239.  
  240.     if (!splitsize)
  241.       splitsize = index->splitsize;
  242.  
  243.     while (1==1)
  244.     {
  245.       if (!(p = my_fgets (line, 80, rfile)))
  246.         break;
  247.  
  248.       if (itsacor)
  249.       {
  250.         if (p[0] == ' ' && p[1] == 'P')
  251.         {
  252.           if (!(corrpart = get_hex (p+2)))
  253.             break;
  254.           my_fgets (p, 80, rfile);
  255.         }
  256.         corrline = get_hex (p+2);
  257.         my_fgets (p, 80, rfile);
  258.       }
  259.       else
  260.         crc_n_lnum (&crc, &corrline, p);
  261.  
  262.       c_line = ((long) (corrpart-1) * splitsize) + corrline;
  263.  
  264.       /* Check, if that line is needed */
  265.       if (!(index->lines_ok[(int)(c_line>>5)] & (1UL <<(c_line&31L))))
  266.         continue;
  267.  
  268.       /* Get crc from code line */
  269.       crc_n_lnum (&crc, &corrline2, p);
  270.  
  271.       /* Calculate CRC */
  272.       csequence = 0;
  273.       for (i=0; i<64; i++)
  274.         csequence = crctab[csequence>>8] ^ (((csequence&255)<<8) | (byte)p[i]);
  275.       csequence &= 0x3fff; /* strip calculated CRC to 14 bits */
  276.  
  277.       if (csequence != crc)
  278.         if (!rebuild (p, 0))
  279.           /* Incorrect CRC. Ignore line. */
  280.           continue;
  281.         else
  282.           crc_n_lnum (&crc, &corrline2, p);
  283.  
  284.       /* Is it really the right line? */
  285.       if (corrline2 != corrline)
  286.         continue;
  287.  
  288.       /* Calculate offset to metafile and position the read pointer there */
  289.       offset = (long) c_line * 62UL;
  290.       if (offset != (oldoffset + 62UL) || !offset)
  291.         fseek (meta, offset, SEEK_SET);
  292.       oldoffset = offset;
  293.  
  294.       /* Calculate number of valid bytes in the line */
  295.       length = 62;
  296.       if (c_line == ((index->length +61) /62)-1)
  297.       {
  298.         length = (int) index->length % 62;
  299.         if (!length)
  300.           length = 62;
  301.       }
  302.       /* Decode & insert the line into the metafile */
  303.       decode_n_write (meta, p, length);
  304.  
  305.       /* Mark line as present */
  306.       index->lines_ok[(int)(c_line>>5)] &=
  307.                                   (0xffffffffUL - (1UL <<(c_line&31L)));
  308.       index->lines_left--;
  309.       printf ("Processing '%s'. Missing lines left: %ld      \r",
  310.                                                   newname, index->lines_left);
  311.       fflush (stdout);
  312.  
  313.     }
  314.     fclose (rfile);
  315.   }
  316.   printf ("\n");
  317.  
  318.   if (meta)
  319.   {
  320.     #ifdef _CHSIZE_OK
  321.      if (!index->lines_left)
  322.      {
  323.        fseek (meta, 0L, SEEK_SET);
  324.        chsize (fileno(meta), index->length);
  325.      }
  326.     #endif
  327.     fclose (meta);
  328.   }
  329.  
  330. #if (__MSDOS__ || __TOS__)
  331.   p = index->filename;
  332. #else
  333.   p = index->full_name;
  334. #endif
  335.  
  336.   if (index->lines_left)
  337.   {
  338.     if (autokill)
  339.       kill_em (_file, inpath, "cor", "c", NULL, NULL, NULL);
  340.  
  341.     w_index_err (index, 0);
  342.     printf ("\nCorrection of %s not successful.\n", p);
  343.  
  344.     return (16);
  345.   }
  346.  
  347.   test_file (NULLFP, p, 1, MAXFNAME-1);
  348.   replace (p, metafile, index->timestamp);
  349.   #ifndef _CHSIZE_OK
  350.    unlink (indexfile);
  351.   #endif
  352.  
  353.   if (autokill)
  354.     kill_em (_file, inpath, "err", "e", "cor", "c", NULL);
  355.  
  356.   printf ("\nCorrection successful! '%s', %ld bytes.\n", p, index->length);
  357.  
  358.   return (0);
  359. }
  360.